package com.mdd.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.product.entity.CategoryEmployment;
import com.mdd.product.mapper.CategoryEmploymentMapper;
import com.mdd.product.service.ICategoryEmploymentService;
import com.mdd.product.validate.category.CategoryEmploymentCreateValidate;
import com.mdd.product.validate.category.CategoryEmploymentUpdateValidate;
import com.mdd.product.vo.category.CategoryEmploymentDetailVo;
import com.mdd.product.vo.category.CategoryEmploymentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 行业类别实现类
 * @author LikeAdmin
 */
@Service
public class CategoryEmploymentServiceImpl implements ICategoryEmploymentService {

    @Resource
    CategoryEmploymentMapper categoryEmploymentMapper;

    /**
     * 行业类别列表
     *
     * @author LikeAdmin
     * @return List<PmsCategoryEmploymentListedVo>
     */
    @Override
    public List<CategoryEmploymentVo> list() {
        LambdaQueryWrapper<CategoryEmployment> categoryEmployment=new LambdaQueryWrapper<>();

        List<CategoryEmployment> data = categoryEmploymentMapper.selectList(categoryEmployment);

        List<CategoryEmploymentVo> list = new LinkedList<>();
        for(CategoryEmployment item : data) {
            CategoryEmploymentVo vo = new CategoryEmploymentVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return list;
    }

    /**
     * 行业类别详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return CategoryEmployment
     */
    @Override
    public CategoryEmploymentDetailVo detail(Long id) {
        CategoryEmployment model = categoryEmploymentMapper.selectById(id);

        Assert.notNull(model, "行业类别数据不存在");

        CategoryEmploymentDetailVo vo = new CategoryEmploymentDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 行业类别新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(CategoryEmploymentCreateValidate createValidate) {
        CategoryEmployment model = new CategoryEmployment();
        model.setPId(createValidate.getPId());
        model.setName(createValidate.getName());
        model.setLevel(createValidate.getLevel());
        model.setStatus(createValidate.getStatus());
        categoryEmploymentMapper.insert(model);
    }

    /**
     * 行业类别编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(CategoryEmploymentUpdateValidate updateValidate) {
        CategoryEmployment model = categoryEmploymentMapper.selectOne(
                new QueryWrapper<CategoryEmployment>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "行业类别数据不存在!");

        model.setId(updateValidate.getId());
        model.setPId(updateValidate.getPId());
        model.setName(updateValidate.getName());
        model.setLevel(updateValidate.getLevel());
        model.setStatus(updateValidate.getStatus());
        categoryEmploymentMapper.updateById(model);
    }

    /**
     * 行业类别删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        CategoryEmployment model = categoryEmploymentMapper.selectById(id);

        Assert.notNull(model, "行业类别数据不存在!");
        LambdaQueryWrapper<CategoryEmployment> categoryEmployment=new LambdaQueryWrapper<>();
        categoryEmployment.eq(CategoryEmployment::getPId,id);
        Long count = categoryEmploymentMapper.selectCount(categoryEmployment);
            Assert.isTrue(count!=null||count>0,"请先移除行业子类别");

        categoryEmploymentMapper.delete(new QueryWrapper<CategoryEmployment>().eq("id", id));
    }

}
